﻿Imports System
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Ipc
Namespace AppDomains.Communication
    Public Class RemotingClient
        Private Shared mut As New Threading.Mutex()
        Private Shared remoteEntry As WellKnownClientTypeEntry
        Private Shared remoteChannel As IpcClientChannel
        Shared chan As Channels.Tcp.TcpChannel
        Shared obj As RemotingObject
        Shared cwork As CoreFX.AppDomains.ConnectionWorker
        Public Overloads Function InitializeLifetimeService() As Object
            Return Nothing
        End Function
        <Obsolete("Function is being replaced by SubscribeToBroadcast", False)> _
        Public Shared Function CreateRemote(ByVal Server As String) As RemotingObject
            Dim remoteUrl As String = Server & "/" & Server
            Dim props As Collections.IDictionary = New Collections.Hashtable
            If remoteChannel Is Nothing OrElse remoteEntry Is Nothing Then
                mut.WaitOne()
                Try
                    If remoteChannel Is Nothing Then
                        remoteChannel = New IpcClientChannel()
                        ChannelServices.RegisterChannel(remoteChannel, True)
                    End If
                    If remoteEntry Is Nothing Then
                        remoteEntry = New WellKnownClientTypeEntry(GetType(RemotingObject), remoteUrl)
                        RemotingConfiguration.RegisterWellKnownClientType(remoteEntry)
                    End If
                Finally
                    mut.ReleaseMutex()
                End Try
            End If
            Try
                Dim obj As RemotingObject = DirectCast(Activator.GetObject(remoteEntry.ObjectType, remoteUrl), RemotingObject)
                Return obj
            Catch e As Exception
                Throw e
            End Try
        End Function
        Public Function StartClient(ByVal Server As String) As Boolean
            obj = CreateRemote(Server)
            If Not IsNothing(obj) Then Return True
        End Function
        Public Sub Connected()
            If Not IsNothing(obj) Then obj.CreateNewConnection(AppDomain.CurrentDomain.FriendlyName)
        End Sub
        Public Sub NewChannel(ByVal ChannelName As String, ByVal ServerName As String, ByVal ChannelObj As ChatEngine.Client.Channel.IRCChannel)
            If Not IsNothing(obj) Then obj.NewChannel(ChannelName, ServerName, ChannelObj)
        End Sub
        Public Sub ChangeNick(ByVal PrevNick As String, ByVal NewNick As String, ByVal ServerName As String)
            If Not IsNothing(obj) Then obj.ChangeNick(PrevNick, NewNick, ServerName)
        End Sub
        Public Sub ChangeTopic(ByVal Topic As String, ByVal ChannelName As String, ByVal ServerName As String)
            If Not IsNothing(obj) Then obj.ChangeTopic(Topic, ChannelName, ServerName)
        End Sub
        Public Sub ChangeUserMode(ByVal E As ChatEngine.Connections.UserModeArgs, ByVal ServerName As String)
            If Not IsNothing(obj) Then obj.ChangeUserMode(E, ServerName)
        End Sub
        Public Sub ChannelModeChanged(ByVal E As ChatEngine.Connections.ChannelModeArgs, ByVal ServerName As String)
            If Not IsNothing(obj) Then obj.ChannelModeChanged(E, ServerName)
        End Sub
        Public Sub Disconnect()
        End Sub
        Public Sub HandleNotice(ByVal E As ChatEngine.Connections.GenericEventArgs, ByVal ServerName As String)
            If Not IsNothing(obj) Then obj.HandleNotice(E, ServerName)
        End Sub
        Public Sub Kicked(ByVal E As ChatEngine.Connections.KickedEventArgs, ByVal ServerName As String)
            If Not IsNothing(obj) Then obj.Kicked(E, ServerName)
        End Sub
        Public Sub LoadNickList(ByVal Channel As String, ByVal E As ChatEngine.Connections.NickListRecvArgs, ByVal ServerName As String)
            If Not IsNothing(obj) Then obj.LoadNickList(Channel, E, ServerName)
        End Sub
        Public Sub NewMessage(ByVal User As String, ByVal Message As String, ByVal ChannelName As String, ByVal ServerName As String)
            If Not IsNothing(obj) Then obj.NewMessage(User, Message, ChannelName, ServerName)
        End Sub
        Public Sub NewQuery(ByVal User As String, ByVal FirstMsg As String, ByVal IsFirstMessage As Boolean, ByVal ServerName As String)

        End Sub
        Public Sub PartFromChannel(ByVal User As String, ByVal Channel As String, ByVal ServerName As String)
            If Not IsNothing(obj) Then obj.PartFromChannel(User, Channel, ServerName)
        End Sub
        Public Function RenderMessage(ByVal Message As String, ByVal ChannelName As String, ByVal User As String, ByVal ServerName As String) As String
            Return Nothing
        End Function
        Public Sub UserJoined(ByVal User As String, ByVal ChannelName As String, ByVal ServerName As String)
            If Not IsNothing(obj) Then obj.UserJoined(User, ChannelName, ServerName)
        End Sub
        Public Sub UserQuit(ByVal User As String, ByVal Reason As String, ByVal ServerName As String)
            If Not IsNothing(obj) Then obj.UserQuit(User, Reason, ServerName)
        End Sub
        Public Sub AddConsoleLine(ByVal Info As String)
            If Not IsNothing(obj) Then obj.AddConsoleLine(Info)
        End Sub
    End Class
End Namespace